package com.paas.form.service;


import com.paas.common.util.BeanUtil;
import com.paas.common.util.StringUtil;
import com.paas.form.entity.FormViewChangeInfoDetailEntity;
import com.paas.form.vo.ChangeField;
import org.apache.commons.lang3.reflect.FieldUtils;

import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * 变更接口抽象类
 */
public abstract class AbsChangeService {

    public abstract Map<String, List<ChangeField>> getViewChangeFieldConfig();

    /**
     *
     * @param type 变更分类 A新增 M修改 D删除
     * @param linkId 数据id
     * @param linkTable
     * @param fieldName 变更描述
     * @param detailList 变更明细集合
     */
    protected void addOrDelChangeDetail(String type,String linkId,String linkTable,String fieldName,List<FormViewChangeInfoDetailEntity> detailList){
        FormViewChangeInfoDetailEntity detail = new FormViewChangeInfoDetailEntity();
        detail.setType(type);
        detail.setLinkId(linkId);
        detail.setLinkTable(linkTable);
        //设置变更字段 中文名称
        detail.setFieldName(fieldName);
        detailList.add(detail);
    }


    /**
     * 设置变更明细
     * @param oldObject 变更前数据库对象
     * @param newObject 当前变更提交对象
     * @param linkId 变更对象主键
     * @param linkTable 变更详细定位到具体表
     * @param detailList 变更明细存储集合
     * @throws NoSuchMethodException
     * @throws SecurityException
     * @throws Exception
     */
    protected void compareChangeInfo(Object oldObject,Object newObject,String linkId, String linkTable,List<FormViewChangeInfoDetailEntity> detailList) throws NoSuchMethodException, SecurityException, Exception{

        Map<String,List<ChangeField>> viewChangeFieldConfigMap = getViewChangeFieldConfig();
        //无变更配置项时，直接结束
        if(viewChangeFieldConfigMap == null || viewChangeFieldConfigMap.size() ==0){
            return ;
        }
        if(!viewChangeFieldConfigMap.containsKey(linkTable) || BeanUtil.isEmpty(viewChangeFieldConfigMap.get(linkTable))){
            return ;
        }

        //如果 新值或旧值存在空值，则不做对比
        if(BeanUtil.isEmpty(oldObject) || BeanUtil.isEmpty(newObject)){
            return ;
        }
        List<ChangeField> changeFieldList =viewChangeFieldConfigMap.get(linkTable);

        // 旧值
        String oldVal;
        // 新值
        String newVal;
        //时间格式
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        //变更明细
        FormViewChangeInfoDetailEntity detail = null ;
        /**
         * 迭代循环变更配置
         */
        for(ChangeField changeField:changeFieldList){
            oldVal = null;
            newVal = null;
            boolean isExistsChange = false; //标识是否存在变更
            //获取属性对应的File
            Field field = FieldUtils.getField(oldObject.getClass(), changeField.getCode(),true);
            if(field == null){
                throw new RuntimeException(changeField.getCode()+"获取属性对象失败");
            }
            //获取旧值对应方法名称
            Method oldMethod = (Method) oldObject.getClass().getMethod("get" + getMethodName(changeField.getCode()));
            //获取新增对应方法
            Method newMethod = (Method) newObject.getClass().getMethod("get" + getMethodName(changeField.getCode()));
            // 如果类型是Date
            if(field.getGenericType().toString().equals("class java.util.Date")){
                Date oldValDate = (Date) oldMethod.invoke(oldObject);
                Date newValDate = (Date) newMethod.invoke(newObject);
                //赋值
                if(BeanUtil.isNotEmpty(oldValDate)){
                    oldVal = sdf.format(oldValDate);
                }
                if(BeanUtil.isNotEmpty(newValDate)){
                    newVal = sdf.format(newValDate);
                }
            }else{
                if(BeanUtil.isNotEmpty(oldMethod.invoke(oldObject))){
                    oldVal = oldMethod.invoke(oldObject).toString();
                }
                if(BeanUtil.isNotEmpty(newMethod.invoke(newObject))){
                    newVal = newMethod.invoke(newObject).toString();
                }
            }
            /**
             * 校验新旧值改变
             */
            if(StringUtil.isEmpty(oldVal) && StringUtil.isEmpty(newVal)){
                //都为空 不变
                continue;
            }else if(StringUtil.isEmpty(oldVal) || StringUtil.isEmpty(newVal)){
                isExistsChange =true;

            }else if(!oldVal.equals(newVal)){
                isExistsChange =true;
            }
            // 如果存在变更添加变更项
            if(isExistsChange){
                detail = new FormViewChangeInfoDetailEntity();
                detail.setType("M");
                detail.setLinkId(linkId);
                detail.setLinkTable(linkTable);
                detail.setFieldCode(changeField.getCode());
                detail.setOldValue(oldVal);
                detail.setNewValue(newVal);
                //设置变更字段 中文名称
                detail.setFieldName(changeField.getName());
                detailList.add(detail);
            }
        }
    }

    // 把一个字符串的第一个字母大写、效率是最高的、
    private static String getMethodName(String fildeName) throws Exception{
        return StringUtil.makeFirstLetterUpperCase(fildeName);
    }

}
